local mysql = require "resty.mysql"

local db, err = mysql:new()
if not db then
    ngx.say(err)
    return
end

db:set_timeout(1000)

local ok, err, errcode, sqlstate = db:connect{
    host = "192.168.1.20",
    port = 3306,
    database = "test",
    user = "root",
    password = "1234",
    charset = "utf8",
    max_packet_size = 1024 * 1024
}
if not ok then
    ngx.say(err, errcode, sqlstate)
    return
end

local res, err, errcode, sqlstate =
    db:query("select * from region limit 10", 10)
if not res then
    ngx.say(err, errcode, sqlstate)
    return
end

local cjson = require "cjson"
ngx.say('<meta charset="utf-8">')
ngx.say(cjson.encode(res))
-- res.affected_rows
-- res.insert_id

local ok, err = db:close()
if not ok then
    ngx.say(err)
    return
end
